package com.reggie.serve;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@ServletComponentScan
@EnableTransactionManagement
public class ServeApplication {

    private static final Logger log = LoggerFactory.getLogger(ServeApplication.class);

    public static void main(String[] args) {
        try {
            ConfigurableApplicationContext context = SpringApplication.run(ServeApplication.class, args);
            ConfigurableEnvironment environment = context.getEnvironment();
            String port = environment.getProperty("server.port", "8080"); // 默认端口设置为8080
            String path = normalizePath(environment.getProperty("server.servlet.context-path"));

            log.info("ServeApplication started");
            log.info("\n----------------------------------------------------------\n\tApplication spring-Boot is running! Access URLs:\n\tLocal: \t\thttp://localhost:{}{}/\n\t----------------------------------------------------------", port, path);
        } catch (RuntimeException e) {
            log.error("Application startup failed", e);
            System.exit(1);
        }
    }

    private static String normalizePath(String path) {
        return ("/".equals(path) || path == null) ? "" : path;
    }
}
